import 'package:app/pages/login_modules/register_module/register_getcode_view.dart';
import 'package:app/router/app_pages.dart';
import 'package:bot_toast/bot_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

import '../../../services/address.dart';
import '../../../services/dio_manager.dart';
import '../login_view.dart';

class RegisterController extends GetxController{



  var tf = TextEditingController();

  var strMobile = '';

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();

    print('phoneStr  ====  ${Get.arguments}');
    // if(Get.arguments!=null){
    //   phoneStr = Get.arguments;
    //
    // }
  }


  /// 发送验证码请求
   Future requestDataWithRegisterSendCode(var mobile)async{

    var params = {
      'mobile':mobile,
      'type':'register'
    };
    var json = await DioManager().kkRequest(Address.sendCode,bodyParams: params,isShowLoad: true);

    return json;

  }

  /// 设置用户名密码请求
   requestDataWithSetPassword(var mobile,var password,var code)async{

    var params = {
      'account_type':'0',
      'user_type':'0',
      'login':mobile.replaceAll(new RegExp(r"\s+\b|\b\s"), ""),
      'name':'',
      'mobile':mobile.replaceAll(new RegExp(r"\s+\b|\b\s"), ""),
      'email':'',
      'password':password,
      'code':code,
      'company_name':'',
    };

    var json = await DioManager().kkRequest(Address.signup,bodyParams: params);

    if(json['errmsg']=='ok'){
      Get.offAll(LoginView());
    }else{
      Get.offAll(LoginView());

    }


  }

   ///点击获取验证码
   tapClickGetCode(){
    if(tf.text.isEmpty){
      // Toast.toast(Get.overlayContext,msg: '请输入正确的手机号码');
      BotToast.showText(text: '请输入手机号码');

      return;
    }
    if(!GetUtils.isPhoneNumber(strMobile)){
      BotToast.showText(text: '请输入正确的手机号码');
      return;
    }

    requestDataWithRegisterSendCode(tf.text.replaceAll(new RegExp(r"\s+\b|\b\s"), "")).then((value){
      if(value['errcode']!=-1){
        Get.to(RegisterGetCodeView(phoneStr:strMobile));
        // Get.toNamed(AppRoutes.registerGetCode,arguments: strMobile);

        BotToast.showText(text: value['message']);
      }else{
        BotToast.showText(text: value['message']);
      }
    });
  }
  /// 跳转设置密码界面
  //  pushToSetPassWord(var codeStr){
  //   Get.toNamed(AppRoutes.registerSetPass,arguments:{'mobile':phoneStr,'code':codeStr});
  //  }

}